package com.lionxing.DNTG;

import java.io.File;
import java.io.FileNotFoundException;

import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.menus.CCMenu;
import org.cocos2d.menus.CCMenuItemImage;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.opengl.CCBitmapFontAtlas;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGSize;

import android.provider.MediaStore;

public class completeLayer extends CCLayer {
	
	
	int _value,_step;
	
	public static CCScene scene(int pic, int step) {
		
		
		CCScene scene = CCScene.node();
		completeLayer layer = new completeLayer(pic,step);

		scene.addChild(layer);
		
		return scene;
	}
	
	
	protected completeLayer()
	{
		
		
	}
	
	protected completeLayer(int pic,int step)
	{
		_value = pic;
		_step = step;
		
		if(pic > 72)
		{
			pic = 72;
			
		}
		
		String filename = String.format("%d.png", pic);
		
		CGSize size = CCDirector.sharedDirector().winSize();
		
		CCSprite bgSp = CCSprite.sprite(filename);
		
		
//	    CCSprite* menuBg = [CCSprite spriteWithFile:@"pass.png"];
//	    menuBg.position = ccp(160, 240);
		
		
		CCSprite menuBg = CCSprite.sprite("pass.png");
		menuBg.setPosition(CGPoint.make(160, 240));
		
		bgSp.addChild(menuBg);
		
		
		CCMenuItemImage menuitemsave = CCMenuItemImage.item("passba.png", "passb.png", "passbaa.png", this, "saveMMtoPhoen");

		CCMenu menusave = CCMenu.menu(menuitemsave);
		menusave.setPosition(80, 95);
		
		boolean aIssave=  Boolean.parseBoolean(filemanager.sharedDirector().getHashmapmmdat().get(filename).toString());
		
		if(!filemanager.sharedDirector().isSDexist() || aIssave)
		{
			menuitemsave.setIsEnabled(false);
		}
		
		CCMenuItemImage menuitemretry = CCMenuItemImage.item("passca.png", "passc.png",  this, "retrymm");

		CCMenu menuretry = CCMenu.menu(menuitemretry);
		menuretry.setPosition(68, 40);
		
		
		CCMenuItemImage menuitemnext = CCMenuItemImage.item("passaa.png", "passa.png",  this, "nextMM");

		CCMenu menunext = CCMenu.menu(menuitemnext);
		menunext.setPosition(200, 60);
		

		

		CCMenuItemImage menuitembacktoleve = CCMenuItemImage.item("passda.png", "passd.png",  this, "gotoMMlistView");

		CCMenu menubackleve = CCMenu.menu(menuitembacktoleve);
		menubackleve.setPosition(160, 130);
		
		CCMenuItemImage menuitemShowpic = CCMenuItemImage.item("passe.png", "passe.png",  this, "showPic");

		CCMenu menushowpic = CCMenu.menu(menuitemShowpic);
		menushowpic.setPosition(270, 130);
		
		menuBg.addChild(menusave);
		menuBg.addChild(menuretry);
		menuBg.addChild(menunext);
		
		bgSp.addChild(menubackleve);
		bgSp.addChild(menushowpic);
		
		bgSp.setScaleX((size.width/GameDefine.KIos_W));
		bgSp.setScaleY((size.height/GameDefine.KIos_h));
		bgSp.setPosition(CGPoint.make(size.width/2, size.height/2));
		
	    int ascore = 0;
	    
	    if (step < GameDefine.MAX_STEP) {
	        
	        ascore = GameDefine.MAX_SCORE;
	    }
	    else
	    {
	        
	       ascore=  GameDefine.MAX_SCORE - (step - GameDefine.MAX_STEP)*100;
	    }
    	String levestring= 	String.format("%d",ascore);
    	String stepstring= 	String.format("%d",_step);
		
    	CCBitmapFontAtlas label1 = CCBitmapFontAtlas.bitmapFontAtlas(levestring, "01.fnt");
    	CCBitmapFontAtlas label2 = CCBitmapFontAtlas.bitmapFontAtlas(stepstring, "01.fnt");
//    	
//    	lbScore.position = ccp(160, 195);
//        lbStep.position = ccp(160, 148);
    	label1.setAnchorPoint(1.0f, 1.0f);
    	label1.setScale(0.8f);
    	label2.setAnchorPoint(1.0f, 1.0f);
    	label2.setScale(0.8f);
    	label1.setPosition(160, 195);
    	label2.setPosition(160, 148);
    	menuBg.addChild(label1);
    	menuBg.addChild(label2);
		//Log.
		addChild(bgSp, -1);
		
		if(_value < 72)
		{
			String tmp2 = String.format("%d.png", _value+1);
			filemanager.sharedDirector().getHashmapmmdat().put(tmp2+"step", step);
		}
		
		String tmp = String.format("%d.png", _value);
		filemanager.sharedDirector().getHashmapmmdat().put(tmp+"step", step);
		
		
	}

	
	public void saveMMtoPhoen(Object sender)
	{
		playSound.sharedDirector().playEffectSound(R.raw.kick);
		filemanager.sharedDirector().savepictoSD(_value);
		String filename = String.format("%d.png", _value);
		filemanager.sharedDirector().getHashmapmmdat().put(filename, true);
		
	}
	
	public void retrymm(Object sender)
	{
		playSound.sharedDirector().playEffectSound(R.raw.kick);
		CCScene scene = PlayLayer.scene(_value);
		// CCDirector.sharedDirector().replaceScene(scene);
		 
		 scenemanager.go(scene);
		
	}
	
	public void nextMM(Object sender)
	{
		playSound.sharedDirector().playEffectSound(R.raw.kick);
		if(_value < 72)
		{
			filemanager.sharedDirector().saveHashMaptofile();
			CCScene scene = PlayLayer.scene(_value+1);
			 //CCDirector.sharedDirector().replaceScene(scene);
			 scenemanager.go(scene);
		}
		
		
	}
	
	public void gotoMMlistView(Object sender)
	{
		playSound.sharedDirector().playEffectSound(R.raw.kick);
		filemanager.sharedDirector().saveHashMaptofile();
		CCScene scene = leveLayer.scene(1);
		// CCDirector.sharedDirector().replaceScene(scene);
		 scenemanager.go(scene);
		
		
	}
	
	public void showPic(Object sender)
	{
		playSound.sharedDirector().playEffectSound(R.raw.kick);
 		CCScene scene = showPicLayer.scene(_value,_step);
 		 //CCDirector.sharedDirector().replaceScene(scene);
 		 scenemanager.go(scene);
	}
	
	 public  void onExit()
		{		
				this.removeAllChildren(true);
				super.onExit();
		}
}
